package com.example.tongue.ui.Online.Body;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.tongue.ui.Online.Body.utils.ScoreCalculationEight;
import com.example.tongue.R;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class PhlegmQuality extends Activity implements View.OnClickListener {
    private Button next4,whetherPhlegm;
    private RadioGroup chestTightness,heavyBody,
            fatBelly,foreheadFat,
            swollenEyelids,stickyMouth,
            phlegm,thickAndGreasyTongueCoating;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.phlegmquality);
        //控件绑定资源
        next4=findViewById(R.id.Next4);

                    chestTightness=findViewById(R.id.ChestTightness);
                    heavyBody=findViewById(R.id.HeavyBody);
                    fatBelly=findViewById(R.id.FatBelly);
                    foreheadFat=findViewById(R.id.ForeheadFat);
                    swollenEyelids=findViewById(R.id.SwollenEyelids);
                    stickyMouth=findViewById(R.id.StickyMouth);
                    phlegm=findViewById(R.id.Phlegm);
                    thickAndGreasyTongueCoating=findViewById(R.id.ThickAndGreasyTongueCoating);
        //设置监听器
        next4.setOnClickListener(this);

                    chestTightness.setOnClickListener(this);
                    heavyBody.setOnClickListener(this);
                    fatBelly.setOnClickListener(this);
                    foreheadFat.setOnClickListener(this);
                    swollenEyelids.setOnClickListener(this);
                    stickyMouth.setOnClickListener(this);
                    phlegm.setOnClickListener(this);
                    thickAndGreasyTongueCoating.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.Next4:{
                //计算结果,把结果写入缓存
                PhlegmResult();

//                Toast.makeText(PhlegmQuality.this, "进入湿热质", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(PhlegmQuality.this,HeatAndHumidity.class));
            }
            break;

        }

    }
    public void PhlegmResult(){
        int  chestTightnessid= chestTightness.getCheckedRadioButtonId();
        RadioButton chestTightnessbtn= findViewById(chestTightnessid);
        String chestTightnessfin=chestTightnessbtn.getText().toString();

        int    heavyBodyid=   heavyBody.getCheckedRadioButtonId();
        RadioButton heavyBodybtn= findViewById(heavyBodyid);
        String heavyBodyfin=heavyBodybtn.getText().toString();

        int  fatBellyid=  fatBelly.getCheckedRadioButtonId();
        RadioButton  fatBellybtn= findViewById( fatBellyid);
        String   fatBellyfin=  fatBellybtn.getText().toString();

        int foreheadFatid= foreheadFat.getCheckedRadioButtonId();
        RadioButton foreheadFatbtn= findViewById(foreheadFatid);
        String foreheadFatfin=foreheadFatbtn.getText().toString();

        int  swollenEyelidsid= swollenEyelids.getCheckedRadioButtonId();
        RadioButton  swollenEyelidsbtn= findViewById( swollenEyelidsid);
        String  swollenEyelidsfin= swollenEyelidsbtn.getText().toString();


        int    stickyMouthid=   stickyMouth.getCheckedRadioButtonId();
        RadioButton    stickyMouthbtn= findViewById(   stickyMouthid);
        String    stickyMouthfin=   stickyMouthbtn.getText().toString();

        int phlegmid=phlegm.getCheckedRadioButtonId();
        RadioButton phlegmbtn= findViewById(phlegmid);
        String phlegmfin=phlegmbtn.getText().toString();

        int  thickAndGreasyTongueCoatingid= thickAndGreasyTongueCoating.getCheckedRadioButtonId();
        RadioButton  thickAndGreasyTongueCoatingbtn= findViewById( thickAndGreasyTongueCoatingid);
        String  thickAndGreasyTongueCoatingfin= thickAndGreasyTongueCoatingbtn.getText().toString();




        Map<String,Integer> chestTightnessMap=new HashMap<String,Integer>();
        chestTightnessMap.put("没有",1);
        chestTightnessMap.put("很少",2);
        chestTightnessMap.put("有时",3);
        chestTightnessMap.put("常常",4);
        chestTightnessMap.put("经常",5);

        Map<String,Integer> heavyBodyMap=new HashMap<String,Integer>();
        heavyBodyMap.put("没有",1);
        heavyBodyMap.put("很少",2);
        heavyBodyMap.put("有时",3);
        heavyBodyMap.put("常常",4);
        heavyBodyMap.put("经常",5);

        Map<String,Integer>   fatBellyMap=new HashMap<String,Integer>();
        fatBellyMap.put("没有",1);
        fatBellyMap.put("很少",2);
        fatBellyMap.put("有时",3);
        fatBellyMap.put("常常",4);
        fatBellyMap.put("经常",5);

        Map<String,Integer> foreheadFatMap=new HashMap<String,Integer>();
        foreheadFatMap.put("没有",1);
        foreheadFatMap.put("很少",2);
        foreheadFatMap.put("有时",3);
        foreheadFatMap.put("常常",4);
        foreheadFatMap.put("经常",5);

        Map<String,Integer>  swollenEyelidsMap=new HashMap<String,Integer>();
        swollenEyelidsMap.put("没有",1);
        swollenEyelidsMap.put("很少",2);
        swollenEyelidsMap.put("有时",3);
        swollenEyelidsMap.put("常常",4);
        swollenEyelidsMap.put("经常",5);

        Map<String,Integer> stickyMouthMap=new HashMap<String,Integer>();
        stickyMouthMap.put("没有",1);
        stickyMouthMap.put("很少",2);
        stickyMouthMap.put("有时",3);
        stickyMouthMap.put("常常",4);
        stickyMouthMap.put("经常",5);

        Map<String,Integer> phlegmMap=new HashMap<String,Integer>();
        phlegmMap.put("没有",1);
        phlegmMap.put("很少",2);
        phlegmMap.put("有时",3);
        phlegmMap.put("常常",4);
        phlegmMap.put("经常",5);

        Map<String,Integer> thickAndGreasyTongueCoatingMap=new HashMap<String,Integer>();
        thickAndGreasyTongueCoatingMap.put("没有",1);
        thickAndGreasyTongueCoatingMap.put("很少",2);
        thickAndGreasyTongueCoatingMap.put("有时",3);
        thickAndGreasyTongueCoatingMap.put("常常",4);
        thickAndGreasyTongueCoatingMap.put("经常",5);





        Integer phlegmresult = chestTightnessMap.get(chestTightnessfin)+
                heavyBodyMap.get(heavyBodyfin) +
                fatBellyMap.get(  fatBellyfin) +
                foreheadFatMap.get(foreheadFatfin)+
                swollenEyelidsMap.get(swollenEyelidsfin)+
                stickyMouthMap.get(stickyMouthfin)+
                phlegmMap.get(phlegmfin)+
                thickAndGreasyTongueCoatingMap.get(thickAndGreasyTongueCoatingfin)
                ;

        ScoreCalculationEight scoreCalculationEight=new ScoreCalculationEight(phlegmresult);
        String ConversionScore=scoreCalculationEight.getResult();
        TextView phResult=(TextView)findViewById(R.id.PhlegmResult);
        phResult.setText("痰湿质结果:"+ConversionScore);

        File dataFileDir=new File(getExternalCacheDir(),"PhysiqueDataFile");
        FileWriter writer=null;
        try {
            //开启写入流
            writer=new FileWriter(dataFileDir+"/PhysiqueData.txt",true);
            writer.write(ConversionScore+"\n");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                System.out.println("数据文件已生成");
            }
        }

    }
}
